<template>
  <div class="register">
    <van-uploader :after-read="afterRead" />
    <img :src="imgUrl" v-if="imgUrl" class="img" alt="" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="nickname"
        name="nickName"
        label="昵称"
        placeholder="昵称"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { reqRegisterAPI } from "../api/user";
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "", // 用户名
      password: "", // 密码
      nickname: "", // 昵称
      imgUrl: "", // 图片
    };
  },
  computed: {},
  watch: {},

  methods: {
    async onSubmit(values) {
      console.log("submit", values);
      const result = await reqRegisterAPI({ ...values, avatar: this.imgUrl });
      console.log(result);
      if (result.code === "success") {
        Toast("注册成功");
        this.$router.push("/login");
      }
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.imgUrl = file.content;
    },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped lang="less">
.register {
  .img {
    width: 100px;
    height: 100px;
  }
}
</style>
